package com.isoft.src.main.java.ui;

import com.isoft.src.main.java.model.Product;
import com.isoft.src.main.java.service.AdminService;
import com.isoft.src.main.java.service.ProductService;

import java.util.Map;
import java.util.Scanner;

public class AdminInterface {
    private AdminService adminService;
    private ProductService productService;

    public AdminInterface(AdminService adminService, ProductService productService) {
        this.adminService = adminService;
        this.productService = productService;
    }

    public void showMenu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("1. 添加商品");
            System.out.println("2. 移除商品");
            System.out.println("3. 更新商品信息");
            System.out.println("4. 查看商品");
            System.out.println("5. 查看报表");
            System.out.println("6. 退出");

            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    addProduct();
                    break;
                case 2:
                    removeProduct();
                    break;
                case 3:
                    updateProduct();
                    break;
                case 4:
                    viewProducts();
                    break;
                case 5:
                    viewReports();
                    break;
                case 6:
                    return;
                default:
                    System.out.println("无效操作");
            }
        }
    }

    private void addProduct() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入商品ID: ");
        String id = scanner.nextLine();
        System.out.print("输入商品名称: ");
        String name = scanner.nextLine();
        System.out.print("输入商品价格: ");
        double price = scanner.nextDouble();
        System.out.print("输入商品库存: ");
        int stock = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        Product product = new Product(id, name, price, stock);
        productService.addProduct(product);
        System.out.println("商品添加成功!");
    }

    private void removeProduct() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入要移除商品的ID: ");
        String id = scanner.nextLine();
        productService.removeProduct(id);
        System.out.println("移除成功!");
    }

    private void updateProduct() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入要更新的商品ID: ");
        String id = scanner.nextLine();
        System.out.print("输入新的商品名称: ");
        String name = scanner.nextLine();
        System.out.print("输入新的商品价格: ");
        double price = scanner.nextDouble();
        System.out.print("输入新的商品库存: ");
        int stock = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        Product product = new Product(id, name, price, stock);
        productService.updateProduct(product);
        System.out.println("商品更新成功!");
    }

    private void viewProducts() {
        Map<String, Product> products = productService.getAllProducts();
        System.out.println("商品列表:");
        for (Product product : products.values()) {
            System.out.println(product);
        }
    }

    private void viewReports() {
        adminService.generateSalesReport();
        adminService.generateInventoryReport();
        System.out.println("报表已总结.");
    }
}
